package test.solution;

import com.google.gson.Gson;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import static org.junit.Assert.assertTrue;

/**
 * Created on 2016/6/24.
 */
@RunWith(BlockJUnit4ClassRunner.class)
public class Solution2 {

    public static final Gson gson = new Gson();

    @Test
    public void test() {
        assertTrue(isUgly(1));
        assertTrue(isUgly(2));
        assertTrue(isUgly(3));
        assertTrue(isUgly(4));
        assertTrue(isUgly(5));
        assertTrue(isUgly(6));
        assertTrue(!isUgly(7));
        assertTrue(isUgly(8));
        assertTrue(!isUgly(14));
        assertTrue(!isUgly(1300000000));
        assertTrue(!isUgly(0));
        assertTrue(!isUgly(-23));
    }

    public boolean isUgly(int num) {
        if (num < 1) {
            return false;
        }

        num = filterN(num, 2);
        num = filterN(num, 3);
        num = filterN(num, 5);

        return num == 1;
    }

    public static int filterN(int m, int n) {
        while (m % n == 0) {
            m = m / n;
        }
        return m;
    }
}
